#include "head.h"

int main(int argc, char* argv[]){
    //./client 192.168.239.130 1234
    ARGS_CHECK(argc, 3);   
    bzero(&order, sizeof(order));
    char path[1024] = {0};
    char command[256] = {0};
    char temp[2048] = {0};
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sockfd, -1, "socket");
    int reuserArg = 1;
    int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuserArg, sizeof(reuserArg));
    ERROR_CHECK(ret, -1, "setsockopt");
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
    serverAddr.sin_port = htons(atoi(argv[2]));

    ret= connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
    int netfd=sockfd;
    ERROR_CHECK(ret, -1, "connect");
    char username[1024] = {0};
    char passwd[1024] = {0};
//    printf("please input your name: \n");
//    read(STDIN_FILENO, username, sizeof(username));
//    username[strlen(username)-1]='\0';
//    fflush(STDIN_FILENO);
//    printf("please input your passwd: \n");
//    read(STDIN_FILENO, passwd,sizeof(passwd));
//    passwd[strlen(passwd)-1]='\0';
//    fflush(STDIN_FILENO);
//    
//   login_user(netfd,username,passwd);       //用户登录
    windows(netfd,username,passwd);
    
    int epfd = epoll_create(1);
    ERROR_CHECK(epfd, -1, "epoll_create");
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = STDIN_FILENO;
    epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &event);
    event.events = EPOLLIN;
    event.data.fd = netfd;
    epoll_ctl(epfd, EPOLL_CTL_ADD, netfd, &event);
    while(1){
        struct epoll_event readyarr[2];
        int readynum = epoll_wait(epfd, readyarr, 2, -1);
        for(int i = 0; i < readynum; ++i){

            if(readyarr[i].data.fd == STDIN_FILENO){
                read(STDIN_FILENO, temp, sizeof(temp));
                temp[strlen(temp)-1]='\0';
                int ret = lexicalanalysis(temp, strlen(temp), netfd);
                if(ret != 0){
                    continue;
                }
//                strcpy(command, strtok(temp, " "));
//                if(strncmp("cd", command, 2) == 0){  //失败返回错误信息
//                    strcpy(path, strtok(NULL, " "));
//                    if(isspace(path[0]) != 0){
//                        int ret = cd_dir(netfd, path);
//                        if(ret != 0){
//                            printf("cd error\n");
//                            continue;
//                        }
//                        printf("%s\n", path);
//                    }
//
//                }else if(strncmp("ls", command, 2) == 0){
//                    int ret = ls_dir(netfd);
//                    if(ret != 0){
//                        printf("ls error\n");
//                        continue;
//                    }
//
//                }else if(strncmp("puts", command, 4) == 0){// gets dir1/dir2 file1
//                    strcpy(path, strtok(NULL, " "));
//                    if(isspace(path[0]) != 0){
//                        char file[1024] = {0};
//                        strcpy(file, strtok(NULL, " "));
//                        int ret = puts_file(netfd, file, path);
//                        if(ret != 0){
//                            printf("puts error\n");
//                            continue;
//                        }
//                    }
//                }else if(strncmp("gets", command, 4) == 0){
//                    strcpy(path, strtok(NULL, " "));
//                    if(isspace(path[0]) != 0){
//                        char file[1024] = {0};
//                        strcpy(file, strtok(NULL, " "));
//                        int ret = gets_file(netfd, file, path);
//                        if(ret != 0){
//                            printf("gets error\n");
//                            continue;
//                        }
//                    }
//                }else if(strncmp("remove", command, 6) == 0){
//                    strcpy(path, strtok(NULL, " "));
//                    if(isspace(path[0]) != 0){
//                        char file[1024] = {0};
//                        strcpy(file, strtok(NULL, " "));
//                        int ret = remove_file(netfd, file, path);
//                        if(ret != 0){
//                            printf("remove error\n");
//                            continue;
//                        }
//                    }
//                }else if(strncmp("pwd", command, 3) == 0){
//                    int ret = pwd(netfd);
//                    if(ret != 0){
//                        printf("pwd error\n");
//                        continue;
//                    }
//                }else if(strncmp("mkdir", command, 5) == 0){
//                    strcpy(path, strtok(NULL, " "));
//                    if(isspace(path[0]) != 0){
//                        char file[1024] = {0};
//                        strcpy(file, strtok(NULL, " "));
//                        int ret = mkdir_dir(netfd, file, path);
//                        if(ret != 0){
//                            printf("mkdir error\n");
//                            continue;
//                        }
//                    }
//                }else if(strncmp("rmdir", command, 5) == 0){
//                    strcpy(path, strtok(NULL, " "));
//                    if(isspace(path[0]) != 0){
//                        char file[1024] = {0};
//                        strcpy(file, strtok(NULL, " "));
//                        int ret = rmdir_dir(netfd, file, path);
//                        if(ret != 0){
//                            printf("rmdir error\n");
//                            continue;
//                        }
//                    }
//                }else{
//                    printf("please input again\n");
//                    continue;
//                }
            }
            else{
            }

        }
    }
    return 0;
}

